package com.zw.yzk.kabu.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseFragment;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.utils.CircleTransform;
import com.zw.yzk.kabu.view.activity.CollectedCosActivity;
import com.zw.yzk.kabu.view.activity.CollectedExhibitionActivity;
import com.zw.yzk.kabu.view.activity.CollectedGoodsActivity;
import com.zw.yzk.kabu.view.activity.CollectedStoresActivity;
import com.zw.yzk.kabu.view.activity.HomePageActivity;
import com.zw.yzk.kabu.view.activity.LightListActivity;
import com.zw.yzk.kabu.view.activity.MainActivity;
import com.zw.yzk.kabu.view.activity.MessageCenterActivity;
import com.zw.yzk.kabu.view.activity.MyConcernedActivity;
import com.zw.yzk.kabu.view.activity.MyFansActivity;
import com.zw.yzk.kabu.view.activity.MyGoodsOrderActivity;
import com.zw.yzk.kabu.view.activity.MyTicketActivity;
import com.zw.yzk.kabu.view.activity.MyTicketOrderActivity;
import com.zw.yzk.kabu.view.activity.SettingsActivity;
import com.zw.yzk.kabu.view.activity.StoreActivity;
import com.zw.yzk.kabu.view.activity.imagedetails.ImageDetailsActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2016/12/28.
 */

public class MeFragment extends BaseFragment {

    @Bind(R.id.head)
    ImageView head;
    @Bind(R.id.nick_name)
    TextView nickName;
    @Bind(R.id.introduction)
    TextView introduction;
    @Bind(R.id.concern_count)
    TextView follow;
    @Bind(R.id.fans_count)
    TextView fans;
    @Bind(R.id.cos_count)
    TextView cos;
    @Bind(R.id.unread)
    View unread;

    private MainActivity activity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) context;
    }

    @Override
    protected int setContent() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initView(View view) {
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        setUserInfo();
        activity.refreshUserInfo();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            activity.refreshUserInfo();
            if (activity.unread.getVisibility() == View.VISIBLE) {
                showMessagePoint(true);
            }

        }
    }

    @Override
    public void initComponent() {

    }

    @OnClick(R.id.settings)
    public void startSettingsActivity() {
        startActivity(new Intent(activity, SettingsActivity.class));
    }

    @OnClick(R.id.head)
    public void modifyUserInfo() {
        Rect rect = new Rect();
        head.getGlobalVisibleRect(rect);
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user != null) {
            ImageDetailsActivity.startDetailsActivity(activity, user.getHeadUrl(), rect);
        }
    }

    @OnClick(R.id.personal_homepage)
    public void startHomePage() {
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user == null) {
            return;
        }
        Intent intent = new Intent(activity, HomePageActivity.class);
        intent.putExtra(ActivityConstant.HOME_PAGE_ID, user.getUserId());
        startActivity(intent);
    }

    @OnClick(R.id.message)
    public void startMessagePage() {
        startActivity(new Intent(activity, MessageCenterActivity.class));
    }

    @OnClick(R.id.concerned_exhibition)
    public void startConcernedExhibitionPage() {
        startActivity(new Intent(activity, CollectedExhibitionActivity.class));
    }

    @OnClick(R.id.my_ticket)
    public void startMyTicketPage() {
        startActivity(new Intent(activity, MyTicketActivity.class));
    }

    @OnClick(R.id.ticket_orders)
    public void startTicketOrderPage() {
        startActivity(new Intent(activity, MyTicketOrderActivity.class));
    }

    @OnClick(R.id.collected_stores)
    public void startCollectedStoresPage() {
        startActivity(new Intent(activity, CollectedStoresActivity.class));
    }

    @OnClick(R.id.collected_goods)
    public void startCollectedGoodsPage() {
        startActivity(new Intent(activity, CollectedGoodsActivity.class));
    }

    @OnClick(R.id.mall_orders)
    public void startGoodsOrderPage() {
        startActivity(new Intent(activity, MyGoodsOrderActivity.class));
    }

    @OnClick(R.id.personal_store)
    public void startStorePage() {
        Intent intent = new Intent(activity, StoreActivity.class);
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user != null) {
            intent.putExtra(ActivityConstant.STORE_OWNER_ID, user.getUserId());
        }

        if (user == null || !user.getHasShop()) {
            new AlertDialog.Builder(activity)
                    .setCancelable(true)
                    .setTitle(R.string.no_shop_title)
                    .setMessage(R.string.no_shop_message)
                    .setPositiveButton(R.string.confirm, null)
                    .show();
        } else {
            startActivity(intent);
        }

    }

    @OnClick(R.id.layout_fans_count)
    public void startMyFansPage() {
        startActivity(new Intent(activity, MyFansActivity.class));
    }

    @OnClick(R.id.layout_concern_count)
    public void startMyConcernPage() {
        startActivity(new Intent(activity, MyConcernedActivity.class));
    }

    @OnClick(R.id.layout_cos_count)
    public void startCollectedCosPage() {
        startActivity(new Intent(activity, CollectedCosActivity.class));
    }

    /**
     * 设置个人信息
     */
    public void setUserInfo() {
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user == null) {
            return;
        }
        try {
            //设置头像
            Glide.with(activity)
                    .load(user.getHeadUrl())
                    .centerCrop()
                    .error(R.drawable.ic_default_head)
                    .transform(new CircleTransform(activity))
                    .into(head);
            //设置昵称
            nickName.setText(user.getNickName());
            //设置个性签名
            introduction.setText(user.getIntroduction());
            //设置我的关注
            follow.setText(String.valueOf(user.getTotalFollow()));
            //设置我的粉丝
            fans.setText(String.valueOf(user.getTotalFans()));
            //设置收藏的cos
            cos.setText(String.valueOf(user.getTotalCollectCosplay()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否展示小红点
     */
    public void showMessagePoint(boolean show) {
        if (unread != null) {
            unread.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }
}
